package com.simdo.g768.Dialog;

import static com.simdo.g768.MainActivity.device;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.CheckBox;
import android.widget.Spinner;
import android.widget.Switch;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.g768.Bean.UeidBean;
import com.simdo.g768.Adapter.BulkImsiAdapter;
import com.simdo.g768.Adapter.ImsiAdapter;
import com.simdo.g768.Bean.GnbBean;
import com.simdo.g768.Bean.IMSIState;
import com.simdo.g768.Bean.ImsiBean;
import com.simdo.g768.Bean.MyUeidBean;
import com.simdo.g768.Listener.BeanClickListener;
import com.simdo.g768.Listener.ListItemListener;
import com.simdo.g768.MainActivity;
import com.simdo.g768.R;
import com.simdo.g768.Util.GsonUtil;
import com.simdo.g768.Util.PrefUtil;

import java.util.ArrayList;
import java.util.List;

public class BulkOperationDialog extends Dialog {

    private final Context mContext;
    private final int sortMode;

    public BulkOperationDialog(Context context, int sortMode) {
        super(context, R.style.Theme_B97621);
        this.mContext = context;
        this.sortMode = sortMode;

        View view = View.inflate(context, R.layout.dialog_bulk_operation, null);
        this.setContentView(view);
        this.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //this.setCancelable(false);   // 返回键不消失
        //设置dialog大小，这里是一个小赠送，模块好的控件大小设置
        Window window = this.getWindow();
        window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        window.setNavigationBarColor(Color.parseColor("#2A72FF"));
        //window.setGravity(Gravity.BOTTOM); //可设置dialog的位置
        window.getDecorView().setPadding(0, 0, 0, 0); //消除边距
        //StatusBarUtil.setLightStatusBar(window, true);
        WindowManager.LayoutParams lp = window.getAttributes();

        lp.width = WindowManager.LayoutParams.MATCH_PARENT;   //设置宽度充满屏幕
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setAttributes(lp);

        initView(view);
    }
    private void initView(View root) {
        root.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        CheckBox cb_all = root.findViewById(R.id.cb_all);

        // 列表
        RecyclerView rv_list = root.findViewById(R.id.rv_list);
        List<ImsiBean> mImsiList = new ArrayList<>(MainActivity.getInstance().getImsiList());
        BulkImsiAdapter mImsiAdapter = getBulkImsiAdapter(mImsiList, cb_all);
        rv_list.setLayoutManager(new LinearLayoutManager(mContext));
        rv_list.setAdapter(mImsiAdapter);

        root.findViewById(R.id.btn_ok).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isUpdate = false;
                for (ImsiBean bean : mImsiList) {
                    if (bean.isCheck()) {
                        isUpdate = true;
                        MainActivity.getInstance().addBlackList(bean.getImsi().substring(11), bean.getImsi());
                    }
                }
                if (isUpdate){
                    PrefUtil.build().putString("black_list", GsonUtil.createJsonString(MainActivity.getInstance().getBlackList()));
                    MainActivity.getInstance().sendBlackListToDev();
                    MainActivity.getInstance().showToast(mContext.getString(R.string.added_white_list));
                }
                dismiss();
            }
        });

        cb_all.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean checked = cb_all.isChecked();
                for (ImsiBean bean : mImsiList) {
                    bean.setCheck(checked);
                }
                mImsiAdapter.notifyDataSetChanged();
            }
        });
    }

    private @NonNull BulkImsiAdapter getBulkImsiAdapter(List<ImsiBean> mImsiList, CheckBox cb_all) {
        BulkImsiAdapter mImsiAdapter = new BulkImsiAdapter(mContext, mImsiList, new ListItemListener() {
            @Override
            public void onItemClickListener(int position) {
                boolean check = true;
                for (ImsiBean imsiBean : mImsiList) {
                    if (!imsiBean.isCheck()){
                        check = false;
                        break;
                    }
                }
                cb_all.setChecked(check);
            }
        });
        mImsiAdapter.setSortMode(sortMode);
        return mImsiAdapter;
    }
}
